//
//  LMAlertView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/12/26.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "LMAlertView.h"
@interface LMAlertView()

@property (nonatomic,strong)UIWindow *alertWindow;
@property (nonatomic,strong)FL_Button *lastBtn;

@end


@implementation LMAlertView

-(instancetype)init{
    self=[super init];
    if (self) {
        self.infoDic=[NSMutableDictionary dictionary];
        self.infoDic[@"paymentType"]=nil;
        self.infoDic[@"arrivalName"]=nil;
        self.infoDic[@"arrivalPhone"]=nil;
        self.infoDic[@"arrivalTime"]=nil;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setTime:) name:@"timeSet" object:nil];
        self.backgroundColor=[UIColor colorWithWhite:.3 alpha:.7];
        self.frame=CGRectMake(0, 0, MainW, MainH-64);
        self.whiteView=[[UIView alloc]init];
        self.whiteView.backgroundColor=[UIColor whiteColor];
        ViewRadius(self.whiteView, 6);
        self.whiteView.bounds=CGRectMake(0, 0, 260, 210);
        self.whiteView.center=self.center;
        [self addSubview:self.whiteView];
        
        UILabel*setLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, 20, 30, 15)];
        setLabel.font=FontAndStyle12;
        setLabel.textColor=TextColor6;
        setLabel.text=@"到店时间";
        CGFloat width=[self getLabelWidth:setLabel];
        setLabel.frame=CGRectMake(16, 20, width, 15);
        [self.whiteView addSubview:setLabel];
        
        self.setTime=[[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusLeft];
        [self.setTime setTitle:@"请选择到店时间" forState:0];
        self.setTime.frame=CGRectMake(setLabel.bsg_right+30, 17.5, self.whiteView.bsg_width-56-setLabel.bsg_width, 20);
        [self.setTime setTitleColor:TextColor6 forState:0];
        self.setTime.titleLabel.font=FontAndStyle12;
        self.setTime.tag=10;
        [self.setTime addTarget:self action:@selector(setTimeTogoClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteView addSubview:self.setTime];
        
        UILabel*typeLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, setLabel.bsg_bottom+10, 30, 15)];
        typeLabel.font=FontAndStyle12;
        typeLabel.textColor=TextColor6;
        typeLabel.text=@"付款类型";
        typeLabel.frame=CGRectMake(16, setLabel.bsg_bottom+10, width, 15);
        [self.whiteView addSubview:typeLabel];
        
        self.allPay=[[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusLeft];
        [self.allPay setTitle:@"全款" forState:0];
        self.allPay.frame=CGRectMake(setLabel.bsg_right+30, self.setTime.bsg_bottom+5, self.whiteView.bsg_width/2-23-setLabel.bsg_width/2, 20);
        [self.allPay setTitleColor:TextColor6 forState:0];
        self.allPay.titleLabel.font=FontAndStyle12;
        [self.allPay setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:UIControlStateNormal];
        [self.allPay setImage:[UIImage imageNamed:@"icon_rb_pre"] forState:UIControlStateSelected];
        self.allPay.tag=20;
        [self.allPay addTarget:self action:@selector(setTimeTogoClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteView addSubview:self.allPay];
        
        
        self.somePay=[[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusCenter];
        [self.somePay setTitle:@"定金" forState:0];
        self.somePay.frame=CGRectMake(self.allPay.bsg_right, self.allPay.bsg_y, self.allPay.bsg_width, 20);
        [self.somePay setTitleColor:TextColor6 forState:0];
        self.somePay.titleLabel.font=FontAndStyle12;
        [self.somePay setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:UIControlStateNormal];
         [self.somePay setImage:[UIImage imageNamed:@"icon_rb_pre"] forState:UIControlStateSelected];
        self.somePay.tag=30;
        [self.somePay addTarget:self action:@selector(setTimeTogoClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteView addSubview:self.somePay];
        
        UILabel*getLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, typeLabel.bsg_bottom+10, 30, 15)];
        getLabel.font=FontAndStyle12;
        getLabel.textColor=TextColor6;
        getLabel.text=@"到店人";
        getLabel.frame=CGRectMake(16, typeLabel.bsg_bottom+10, width, 15);
        [self.whiteView addSubview:getLabel];
        self.userName=[[UITextField alloc]initWithFrame:CGRectMake(self.allPay.bsg_x+10, self.somePay.bsg_bottom+5, self.setTime.bsg_width-10, 20)];
        self.userName.font=FontAndStyle12;
        self.userName.textColor=TextColor6;
        self.userName.text=[ZJUserDefaultsTools getCurrentNickName];
        self.infoDic[@"arrivalName"]=self.userName.text;
        [self.whiteView addSubview:self.userName];
        
        
       
        
        
        
        UILabel*numeLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, getLabel.bsg_bottom+10, 30, 15)];
        numeLabel.font=FontAndStyle12;
        numeLabel.textColor=TextColor6;
        numeLabel.text=@"联系电话";
        numeLabel.frame=CGRectMake(16, getLabel.bsg_bottom+10, width, 15);
        [self.whiteView addSubview:numeLabel];
        
        UILabel*infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, getLabel.bsg_bottom+10, 30, 15)];
        infoLabel.font=[UIFont systemFontOfSize:10];
        infoLabel.textColor=TextColor6;
        infoLabel.numberOfLines=0;
        infoLabel.lineBreakMode=NSLineBreakByWordWrapping;
        infoLabel.text=@"备注:确认并支付后将提交订单给该经销商的门店,请耐心等待接单结果.";
        if ([self getLabelWidth:infoLabel]>self.whiteView.bsg_width-26) {
            CGSize size=[infoLabel.text sizeWithAttributes:@{NSFontAttributeName:infoLabel.font }];
            infoLabel.frame=CGRectMake(16, numeLabel.bsg_bottom+10, self.whiteView.bsg_width-26, size.height*2);
        }else{
            infoLabel.frame=CGRectMake(16, numeLabel.bsg_bottom+10, self.whiteView.bsg_width-26, 15);
        }
        
        [self.whiteView addSubview:infoLabel];
        
        
        
        self.numberField=[[UITextField alloc]initWithFrame:CGRectMake(self.allPay.bsg_x+10, self.userName.bsg_bottom+5, self.setTime.bsg_width-10, 20)];
        self.numberField.font=FontAndStyle12;
        self.numberField.textColor=TextColor6;
        self.numberField.text=[ZJUserDefaultsTools getCurrentUserPhone];
        self.infoDic[@"arrivalPhone"]=self.numberField.text;
        [self.whiteView addSubview:self.numberField];
        
        
        
        if (!_cancleBtn) {
            self.cancleBtn=[[UIButton alloc]init];
            [self.cancleBtn setTitle:@"取消" forState:0];
            self.cancleBtn.frame=CGRectMake(-1, self.whiteView.bsg_height-40, self.whiteView.bsg_width/2+2, 40);
            [self.cancleBtn setTitleColor:TextColor3 forState:0];
            self.cancleBtn.titleLabel.font=FontAndStyle14;
            [self.cancleBtn addTarget:self action:@selector(setTimeTogoClick:) forControlEvents:UIControlEventTouchUpInside];
            self.cancleBtn.layer.borderWidth=1;
            self.cancleBtn.tag=40;
            self.cancleBtn.layer.borderColor=Gray_Color.CGColor;
            [self.whiteView addSubview:self.cancleBtn];
        }
        if (!_confirmBtn) {
            self.confirmBtn=[[UIButton alloc]init];
            [self.confirmBtn setTitle:@"确定" forState:0];
            self.confirmBtn.frame=CGRectMake(self.whiteView.bsg_width/2, self.whiteView.bsg_height-40, self.whiteView.bsg_width/2+1, 40);
            [self.confirmBtn setTitleColor:Red_Color forState:0];
            self.confirmBtn.titleLabel.font=FontAndStyle14;
            [self.confirmBtn addTarget:self action:@selector(setTimeTogoClick:) forControlEvents:UIControlEventTouchUpInside];
            self.confirmBtn.layer.borderWidth=1;
            self.confirmBtn.tag=50;
            self.confirmBtn.layer.borderColor=Gray_Color.CGColor;
            [self.whiteView addSubview:self.confirmBtn];
        }
        
    }
    return self;
}
-(CGFloat)getLabelWidth:(UILabel*)label{
    CGSize size=[label.text sizeWithAttributes:@{NSFontAttributeName:label.font }];
    return size.width;
}
-(void)setTime:(NSNotification *)info{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //指定时间显示样式: HH表示24小时制 hh表示12小时制
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *lastTime = info.object;
    NSDate *lastDate = [formatter dateFromString:lastTime];
    //以 1970/01/01 GMT为基准，得到lastDate的时间戳
    long firstStamp = [lastDate timeIntervalSince1970];
    NSLog(@"firstStamp:%ld",firstStamp);

    self.infoDic[@"arrivalTime"]=[NSString stringWithFormat:@"%ld",firstStamp];
}
-(void)setTimeTogoClick:(FL_Button *)sender{
    
    if (sender.tag==10) {
        if ([_delegate respondsToSelector:@selector(chooseTimeClick)]) {
            [_delegate chooseTimeClick];
        }
        //设置时间
    }else if (sender.tag==20){
        if (sender!=self.lastBtn) {
            sender.selected=!sender.selected;
            self.lastBtn.selected=NO;
            self.lastBtn=sender;
        }else{
            sender.selected=YES;
        }
        self.infoDic[@"paymentType"]=@"FULL";
        //全款
    }else if (sender.tag==30){
        if (sender!=self.lastBtn) {
            sender.selected=!sender.selected;
            self.lastBtn.selected=NO;
            self.lastBtn=sender;
        }else{
            sender.selected=YES;
        }
        self.infoDic[@"paymentType"]=@"DEPOSIT";
        //定金
    }else if (sender.tag==40){
        [self disMissAlertView];
        //取消
    }else if (sender.tag==50){
        self.infoDic[@"arrivalName"]=self.userName.text;
        self.infoDic[@"arrivalPhone"]=self.numberField.text;
        if (self.infoDic[@"arrivalTime"]==nil) {
            [MBProgressHUD showError:@"请设置到店时间"];
            return;
        }
        if (self.infoDic[@"paymentType"]==nil) {
            [MBProgressHUD showError:@"请选择付款方式"];
            return;
        }
        if (self.infoDic[@"arrivalName"]==nil||self.infoDic[@"arrivalPhone"]==nil||self.numberField.text.length!=11) {
            [MBProgressHUD showError:@"请完善到店人信息"];
            return;
        }
        
        
        if ([_delegate respondsToSelector:@selector(resultClick:)]) {
            [_delegate resultClick:self.infoDic];
        }
        [self disMissAlertView];
        //确定
    }else if (sender.tag==20){
        
    }
    
}
-(void)disMissAlertView{
    [self removeFromSuperview];
    [_alertWindow resignKeyWindow];
}
-(void)showLXAlertView{
    _alertWindow=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _alertWindow.windowLevel=UIWindowLevelAlert;
    [_alertWindow becomeKeyWindow];
    [_alertWindow makeKeyAndVisible];
    
    [_alertWindow addSubview:self];
    
    [self setShowAnimation];
    
}
-(void)setShowAnimation{
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [_whiteView.layer setValue:@(0) forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.23 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            [_whiteView.layer setValue:@(1.2) forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.09 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                [_whiteView.layer setValue:@(.9) forKeyPath:@"transform.scale"];
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.05 delay:0.02 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    [_whiteView.layer setValue:@(1.0) forKeyPath:@"transform.scale"];
                } completion:^(BOOL finished) {
                    
                }];
            }];
        }];
    }];
}

@end
